package com.yiyi.credit.manage.sso.service.inner;


import com.yiyi.credit.manage.sso.cache.RedisManager;
import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.exception.ServiceException;
import com.yiyi.credit.manage.sso.common.page.PageUtils;
import com.yiyi.credit.manage.sso.common.page.Pagination;
import com.yiyi.credit.manage.sso.mapper.SysInfoMapper;
import com.yiyi.credit.manage.sso.mapper.SysResourceMapper;
import com.yiyi.credit.manage.sso.model.entity.SysInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;


@Service
public class SysInfoServiceImpl implements SysInfoService {

    @Autowired
    SysInfoMapper sysInfoMapper;

    @Autowired
    SysResourceMapper sysResourceMapper;


    @Autowired
    RedisManager redisManager;


    @Override
    public int registerSysInfo(SysInfo sysInfo) throws ServiceException {

        //判断系统名称是否存在
        List<SysInfo> list = getSysInfoByName(sysInfo.getName());
        if (null != list && list.size() > Constant.INT_ZERO) {
            throw new ServiceException(ResponseCode.USER_NOT_EXIST);
        }

        sysInfo.setCreateTime(new Date().getTime());
        sysInfo.setUpdateTime(new Date().getTime());
        return sysInfoMapper.insertSelective(sysInfo);
    }


    @Override
    public int updateSysInfo(SysInfo sysInfo) throws ServiceException {

        //判断系统名称是否存在
        SysInfo checkSysInfo = getSysInfoById(sysInfo.getId());
        if (checkSysInfo == null) {
            throw new ServiceException(ResponseCode.NO_SYS_INFO);
        }
        sysInfo.setUpdateTime(new Date().getTime());
        return sysInfoMapper.  updateByPrimaryKeySelective(sysInfo);
    }


    @Override
    public int deleteSysInfo(SysInfo sysInfo) throws ServiceException {

        //判断系统名称是否存在
        SysInfo checkSysInfo = getSysInfoById(sysInfo.getId());
        if (checkSysInfo == null) {
            throw new ServiceException(ResponseCode.NO_SYS_INFO);
        }

        return sysInfoMapper.deleteByPrimaryKey(sysInfo);
    }


    @Override
    public SysInfo getSysInfoById(Long id) throws ServiceException {

        return sysInfoMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<SysInfo> getSysInfoByName(String name) throws ServiceException {

        Example example = new Example(SysInfo.class);
        example.createCriteria().andEqualTo("name", name);
        return sysInfoMapper.selectByExample(example);
    }







    @Override
    public List<SysInfo> getAllSysInfo() throws ServiceException {
        return sysInfoMapper.selectAll();
    }


    @Override
    public Pagination getSysInfoPage(Pagination pagination) throws ServiceException {
        Long count = sysInfoMapper.getCount();
        pagination.setRecordsTotal(count);
        pagination.setPage(PageUtils.getPage(count, pagination.getLength()));
        if (count > Constant.LONG_ZERO) {
            pagination.setData(sysInfoMapper.getPageData(pagination));
        }
        return pagination;
    }

}
